package tx.hw.demo01;

/**
 * @author lwj
 * @date 2025-03-13
 * @vesion 1.0.0
 */
public class Girl {
    /**
     * 水果数量
     */
    private int fruitNum;

    // 构造方法
    public Girl() {}
    public Girl(int fruitNum) {
        this.fruitNum = fruitNum;
    }

    public void eat() {
        synchronized (MyRunnable.LOCK){
            // try {
            //     // 线程睡眠
            //     MyRunnable.LOCK.wait();
            // } catch (InterruptedException e) {
            //     throw new RuntimeException(e);
            // }
            if(fruitNum >= 5){
                while(true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("我现在来吃水果...");
                    fruitNum -= 1;
                    System.out.println("fruit:"+fruitNum);
                    if(fruitNum == 0){
                        System.out.println("我吃完了，快来摘！");

                        // 唤醒男孩
                        MyRunnable.LOCK.notifyAll();
                        break;
                    }
                }
                try {
                    // 线程睡眠
                    MyRunnable.LOCK.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
